package com.foriseland.fjf.encryption;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;

import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;

import com.foriseland.fjf.encryption.security.BytesToHex;

public class PBKDF2 {

	 public static final String PBKDF2_ALGORITHM = "PBKDF2WithHmacSHA1";  
	   public static final int SALT_BYTE_SIZE = 32 / 2;   // 盐的长度 
	   public static final int HASH_BIT_SIZE = 32 * 4;    // 生成密文的长度 
	   public static final int PBKDF2_ITERATIONS = 15000; // 迭代次数 
	 
	   /** 
	    * 对输入的密码进行验证 
	    *  
	    * @param attemptedPassword  待验证的密码 
	    * @param encryptedPassword  密文 
	    * @param salt  盐值 
	    * @return 是否验证成功 
	    * @throws NoSuchAlgorithmException 
	    * @throws InvalidKeySpecException 
	    */  
	   public static boolean authenticate(String attemptedPassword, String encryptedPassword, String salt)  
	           throws NoSuchAlgorithmException, InvalidKeySpecException {  
	       // 用相同的盐值对用户输入的密码进行加密  
	       String encryptedAttemptedPassword = getEncryptedPassword(attemptedPassword, salt);  
	       // 把加密后的密文和原密文进行比较，相同则验证成功，否则失败  
	       return encryptedAttemptedPassword.equals(encryptedPassword);  
	   }  
	 
	   /** 
	    * 生成密文 
	    *  
	    * @param password 明文密码 
	    * @param salt 盐值 
	    * @throws NoSuchAlgorithmException 
	    * @throws InvalidKeySpecException 
	    */  
	   public static String getEncryptedPassword(String password, String salt) throws NoSuchAlgorithmException,  
	           InvalidKeySpecException {  
	       KeySpec spec = new PBEKeySpec(password.toCharArray(), fromHex(salt), PBKDF2_ITERATIONS,HASH_BIT_SIZE);  
	       SecretKeyFactory f = SecretKeyFactory.getInstance(PBKDF2_ALGORITHM);  
	       byte[] encoded = f.generateSecret(spec).getEncoded();
	       return BytesToHex.bytesToHexString(encoded);  
	   }  
	 
	   /** 
	    * 通过提供加密的强随机数生成器 生成盐 
	    * @return 
	    * @throws NoSuchAlgorithmException 
	    */  
	   public static String generateSalt() throws NoSuchAlgorithmException {  
	       SecureRandom random = SecureRandom.getInstance("SHA1PRNG");  
	       byte[] salt = new byte[SALT_BYTE_SIZE];  
	       random.nextBytes(salt);  
		   
	       return BytesToHex.bytesToHexString(salt);  
	   }  
	   
	   /** 
	    * 十六进制字符串转二进制字符串 
	    * @param   hex         the hex string 
	    * @return              the hex string decoded into a byte array       
	    */  
	   private static byte[] fromHex(String hex) {  
	       byte[] binary = new byte[hex.length() / 2];  
	       for (int i = 0; i < binary.length; i++) {  
	           binary[i] = (byte) Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16);  
	       }  
	       return binary;  
	   }  
	   
	   public static void main(String[] args) throws NoSuchAlgorithmException {
		   String generateSalt = generateSalt();
		   System.out.println(generateSalt);
	}
	 
}  

